package com.wk.service.impl;

import com.wk.annotation.LogAnnotation;
import com.wk.dao.CmfzBannerDao;
import com.wk.entity.CmfzBanner;
import com.wk.service.CmfzBannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class CmfzBannerServiceImpl implements CmfzBannerService {

    @Autowired
    private CmfzBannerDao cmfzBannerDao;

    @LogAnnotation(content = "查询所有轮播图")
    @Override
    public Map findAll(Integer page, Integer limit, String name) {
        Map map = new HashMap();

        page = (page-1)*limit;

        if(name!=null||"".equals(name)){
            name = "%"+name+"%";
        }

        List<CmfzBanner> all = cmfzBannerDao.findAll(page, limit, name);
        log.info("查询所有轮播图数据"+all);
        map.put("data",all);
        Integer count = findCount(name);
        map.put("count",count);
        map.put("msg","");
        map.put("code",0);
        return map;
    }

    @LogAnnotation(content = "查询数量")
    @Override
    public Integer findCount(String name) {
        if(name!=null || "".equals(name)){
            name = "%"+name+"%";
        }
        return cmfzBannerDao.findCount(name);
    }

    @LogAnnotation(content = "添加轮播图",type="insert")
    @Override
    public Map insert(CmfzBanner cmfzBanner) {
        Map map = new HashMap();
        map.put("data","添加失败");
        try {
            cmfzBannerDao.insert(cmfzBanner);
            map.put("data","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("轮播图添加失败");
            throw e;
        }
        return map;
    }

    @LogAnnotation(content = "查询单个轮播图")
    @Override
    public Map findOne(Integer id) {
        Map map = new HashMap();

        CmfzBanner one = cmfzBannerDao.findOne(id);
        log.info("查找单个轮播图"+one);
        map.put("data",one);

        return map;
    }

    @LogAnnotation(content = "修改轮播图",type="update")
    @Override
    public Map update(CmfzBanner cmfzBanner) {

        Map map = new HashMap();

        int update = cmfzBannerDao.update(cmfzBanner);
        if(update>0){
            map.put("data","修改成功");
        }else{
            map.put("data","修改失败");
            log.error("修改轮播图失败");
        }
        return map;
    }

    @LogAnnotation(content = "删除轮播图",type="delete")
    @Override
    public Map delete(Integer id) {

        Map map = new HashMap();
        try{
            cmfzBannerDao.delete(id);
            map.put("data","删除成功");
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }

        return map;
    }
}
